var cfg = require('../../utils/cfg');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasAddress : false,
    good : {},
    buyNum : 1,
    address : {}
  },

  toPay : function() {

    if (!this.data.address.detail) {
      wx.showToast({
        title: '请选择收获地址',
        icon: 'none',
        duration: 2000
      })
        return;
    }

    let orderInfo = {
      openId: wx.getStorageSync('loginUser').openId,
      goodId: this.data.good.goodId,
      realName: this.data.address.username,
      phone: this.data.address.telNumber,
      province: this.data.address.province,
      city: this.data.address.city,
      area: this.data.address.area,
      detail: this.data.address.detail,
      buyNum: this.data.buyNum
    }
    wx.request({
      url: cfg.host + cfg.mwurl.createOrder,
      method : 'POST',
      data: orderInfo,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success : function(res) {
        if (res.data.code !== 1000) {
          console.log('保存失败' + res.data.message);
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
          return;
        }

        console.log('保存成功');
        wx.switchTab({
          url: '/pages/my/my'
        });
      }
    })
  },

  toAddress : function() {
    let that = this;
    console.log(">>>>>address");
    wx.chooseAddress({
      success: function (res) {
        console.log(res.userName)
        console.log(res.postalCode)
        console.log(res.provinceName)
        console.log(res.cityName)
        console.log(res.countyName)
        console.log(res.detailInfo)
        console.log(res.nationalCode)
        console.log(res.telNumber)

        that.setData({ address: { 
          username: res.userName,
          telNumber : res.telNumber,
          province: res.provinceName,
          city : res.cityName,
          area : res.countyName,
          detail : res.detailInfo
          }});
      }
    })
  },

  addBuyNum : function() {
    this.setData({ buyNum: this.data.buyNum + 1});
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log('>>>>>>>goodId=' + options.buyGoodId);
    let goodId = options.buyGoodId;
    //根据goodId 获取商品详情
    wx.request({
      url: cfg.host + cfg.mwurl.loadGood,
      data: {
        goodId: goodId
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.code !== 1000) {
          return;
        }

        let goodInfo = res.data.data;
        that.setData({ good: goodInfo });
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})